global name "main_tables"
cap log close
log using "${pfile}/${name}.smcl", replace smcl



* set baseline bandwidth
global band = 0.075
global bandui = 6
global bandrdbirth = 90


// *** // TABLE 1 // *** //
* time to find a new job
use A_cash, clear
* drop mass points at x=0
drop if x==0

eststo clear
global outcome "sur3 sur36 dur6 dur36"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 

g pre_remmedr = remmedr if n_remmedr!=.
*g pre_remmedia = remmedia if n_remmedia!=.

* reemployment wage and duration
global outcome "n_jdur n_remdezr n_remmedr pre_remmedr"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 

* additional job characteristics
global outcome "n_hor manager whitec migind"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 


// *** // TABLE 2 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* earnings
global outcome "totinc inc12 inc24 inc36"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia

* mon
global outcome "totmon mon12 mon24 mon36"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 

* wage
global outcome "totwage wage12 wage24 wage36"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 


// *** // TABLE 3 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* Tenure groups
g double tenmon = tempempr * (7/30) // tenure measured in months
g g1 = tenmon>=24 & tenmon<42 & dt_dism-lastui> 488 // UI eligible 5 months + low fgts
g g2 = tenmon>=42 & tenmon<60 & dt_dism-lastui> 488 // UI eligible 5 months + high fgts
g g3 = tenmon>=42 & tenmon<99999 & dt_dism-lastui> 488 // UI eligible 5 months + high fgts + very long tenure

foreach gr in 1 2 3 {
eststo clear
global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage" // sur6
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} & g`gr'==1 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01) nonotes title(group `gr')
gstats tab $outcome if g`gr'==1 & x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 
}


// *** // TABLE 4 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* only eligible workers
keep if x<0
drop k kx x

g t = dtb - mdy(7,1,year(dtb))
g k = t>=0
g kt = k * t


* 2011-14
global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kt t if abs(t)<= ${bandrdbirth} , vce(cluster cpf) // [w=(${band}-abs(t))/${band}]
cap gegen av_`var' = mean(`var'), by(tbin)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if t > 0 & t <= ${bandrdbirth}, s(mean) columns(var) // n_remmedia pre_remmedia

* 2014
keep if year==2014
global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kt t if abs(t)<= ${bandrdbirth} , vce(cluster cpf) // [w=(${band}-abs(t))/${band}]
cap gegen av_`var' = mean(`var'), by(tbin)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if t > 0 & t <= ${bandrdbirth}, s(mean) columns(var) // n_remmedia pre_remmedia


// *** // TABLE 5 // *** //
use A_cash_othermonths, clear

* Month of Dismissal grouped
cap drop gd
g gd =  1 if dt_dism>=mdy(1,1,year) & dt_dism<mdy(4,1,year)
replace gd = 2 if dt_dism>=mdy(4,1,year) & dt_dism<mdy(6,1,year)
replace gd = 3 if dt_dism>=mdy(6,1,year) & dt_dism<mdy(7,16,year)
replace gd = 4 if dt_dism>=mdy(7,16,year) & dt_dism<mdy(9,1,year)
replace gd = 5 if dt_dism>=mdy(9,1,year) & dt_dism<=mdy(12,31,year)

foreach gd in 1 2 4 5 {
eststo clear
global outcome "dur36 n_jdur n_remmedr totinc totmon totwage" // sur6
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} & gd==`gd' & dt_dism+und*7>=mdy(6,1,year) [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01) nonotes title(Month of Dismissal group `gd')
gstats tab $outcome if gd==`gd' & x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 
}


// *** // TABLE 6 // *** //
global outcome "sur3 sur6 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"

use A_cash, clear
* drop mass points at x=0
drop if x==0

g double tenmon = tempempr * (7/30) // tenure measured in months
keep if tenmon>=16 & tenmon<=30 // comparable tenure range to ui extension sample

// Selecting variables
keep year id cpf x ${outcome} 

// Cash - RD variables
cap drop k kx
g k = (x<=0 & x!=.)
g kx = k * x

g samplecash = 1

append using B_ui // append data on ui extension analysis
replace samplecash = 0 if samplecash==.
g sampleui = 1 if samplecash==0

replace k = . if samplecash==0
replace kx = . if samplecash==0
replace x = . if samplecash==0

// UI - RD variables
g t = tempempr - 24 if sampleui==1
replace t = . if tempempr > 22  & tempempr <= 24  & sampleui==1
g kui = (t>=0&t!=.) if sampleui==1
g kuit = kui*t if sampleui==1

// Selecting variables
keep year id cpf ${outcome} tempempr w_ebal weight samplecash k kx x sampleui kui t kuit // dt_dism tempempr school ind race gend hor year uf remmedr p_causadesli p_tempempr 


// Deal with common observations across the two samples
foreach var in tempempr w_ebal weight k kx x kui t kuit {
gegen aux`var' = min(`var') , by(year id)
drop `var'
rename aux`var' `var'
}

gegen maxsamplecash = max(samplecash) , by(year id)
gegen maxsampleui = max(sampleui) , by(year id)

sort year id sampleui
by year id: keep if _n==_N

drop samplecash sampleui
rename maxsamplecash samplecash
rename maxsampleui sampleui

keep cpf $outcome samplecash k kx x sampleui kui kuit t tempempr w_ebal

// *** // Cash Bonus Regressions - PANEL A // *** //
global bandcash = .075
eststo clear
foreach var in $outcome { 
qui eststo: reg `var' k kx x if abs(x)<= ${band} & samplecash==1 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x>0 & x <0.05 & samplecash==1, s(mean) columns(var) // n_remmedia pre_remmedia 


// *** // UI Regressions - PANEL B // *** //
global bandui = 6
* time to find a new job - ebalance weights
eststo clear
foreach var in $outcome { 
qui eststo: reg `var' kui kuit t if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} & sampleui==1 [w=w_ebal], vce(cluster cpf)
}
esttab , keep(kui) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if tempempr>= 21 & tempempr<= 22 & sampleui==1 [w=w_ebal], s(mean) columns(var) // n_remmedia pre_remmedia 


// *** // Bootstrapping - PANEL C // *** // Run 9 x 1000 regressions - Take a few hours to run (2:15 on a very good server)
set seed 123456789

// matrix for storing results
set matsize 1000
mat res = J(1000,9,.)
mat coln res = ${outcome} 

preserve
local new = 0
	forval b = 1/1000 {
	local new = `new' + 1
	
	bsample
	
		foreach var in $outcome { // n_remmedia pre_remmedia 
		qui reg `var' k kx x if abs(x)<= ${band} & samplecash==1 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
		scalar coefcash = _b[k] * 1000/((0.8-0.03)*619.4332) // per R$1000 gvt expenditure - 80% FS, 3% recall, 619 bonus
		qui reg `var' kui kuit t if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} & sampleui==1 [w=w_ebal], vce(cluster cpf)
		scalar coefui = _b[kui] * 1000/(0.8*0.83*931.1244) // per R$1000 gvt expenditure - 80% take-up, 83% surv_5, 931 benefits
		mat res[`new',colnumb(res,"`var'")] = abs(coefcash) - abs(coefui)
		}

	restore, pres
	}
	
restore, not
clear
svmat res, names(col)

foreach var in $outcome { // n_remmedia pre_remmedia 
 replace `var' = `var'<=0
}

gcollapse (mean) $outcome 
* p-values
list



// *** // TABLE 7 C17 C18 // *** //
global varmatch "year uf dtbirth gend race large" // metro  race school
global pop = 1000000

use "B2_mergeadmin.dta", clear

merge m:1 cpf using B5_racemode, keep(1 3) nogen keepus(racedismbmodemax)
replace race = racedismbmodemax if race != racedismbmodemax & racedismbmodemax !=.
drop racedismbmodemax

g large = pop>1000000

replace school = school / 100

eststo clear
qui eststo a1: reg emp9 k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)
qui eststo a2: reg s3 k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)
qui eststo a3: reg und k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)
qui eststo a4: reg und_5 k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)

// *** // TABLE 7 - PANEL E // *** //
di "Admin worker level - full sample"
esttab a1 a2 a3 a4, keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab emp9 s3 und und_5 if tempempr >= 21 & tempempr <= 22, s(mean) columns(var)


bys $varmatch: g co = _N
label var co "Cluster Size"
replace co = 50 if co >=50
ta co
keep if co <=10 

eststo clear
qui eststo b1: reg emp9 k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)
qui eststo b2: reg s3 k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)
qui eststo b3: reg und k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)
qui eststo b4: reg und_5 k kx x if tempempr >= 16 & tempempr <= 30, vce(cluster cpf)

// *** // TABLE 7 - PANEL D // *** //
di "Admin worker level - restricted sample"
esttab b1 b2 b3 b4, keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab emp9 s3 und und_5 if tempempr >= 21 & tempempr <= 22, s(mean) columns(var)

*/
				
merge m:1 $varmatch using "B4_pnad_merge_collapse.dta", nogen keep(1 3) keepus(emp_form emp_informal emp_self id_survey p_ui p_ten w w_form w_inf) // race school

g matched = emp_form!=.
g age = (mdy(12,31,year)-dtbirth)/365
g emp_inf2 = emp_info + emp_self

eststo clear
qui eststo d1: reg emp9 k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster cpf)
qui eststo d2: reg s3 k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster cpf)
qui eststo d3: reg und k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster cpf)
qui eststo d4: reg und_5 k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster cpf)

// *** // TABLE 7 - PANEL C // *** //
di "Admin mun-birthdate-gender-race level - matched sample"
esttab d1 d2 d3 d4, keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab emp9 s3 und und_5 if tempempr >= 21 & tempempr <= 22 & matched==1, s(mean) columns(var)

eststo clear
qui eststo e1: reg emp_form k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)
qui eststo e2: reg emp_informal k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)
qui eststo e3: reg emp_self k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)
qui eststo e4: reg emp_inf2 k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)

// *** // TABLE 7 - PANEL A // *** //
di "Admin mun-birthdate-gender-race level - matched sample - survey outcomes"
esttab e1 e2 e3 e4, keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab emp_form emp_informal emp_self emp_inf2 if tempempr >= 21 & tempempr <= 22 & matched==1, s(mean) columns(var)

// number of survey observations used in the regression
distinct id_s if tempempr >= 16 & tempempr <= 30 & x!=.

preserve
cap drop emp_form emp_informal emp_self emp_inf2 matched id_survey
merge m:1 $varmatch using "B4_pnad_merge_collapse_placebo.dta", nogen keep(1 3) keepus(emp_form emp_informal emp_self id_survey p_ui p_ten) // race school

g matched = emp_form!=.
g emp_inf2 = emp_info + emp_self

eststo clear
qui eststo f1: reg emp_form k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)
qui eststo f2: reg emp_informal k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)
qui eststo f3: reg emp_self k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)
qui eststo f4: reg emp_inf2 k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , vce(cluster id_survey)

// *** // TABLE 7 - PANEL B // *** //
di "Admin mun-birthdate-gender-race level - matched sample placebo - survey outcomes"
esttab f1 f2 f3 f4, keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab emp_form emp_informal emp_self emp_inf2 if tempempr >= 21 & tempempr <= 22 & matched==1, s(mean) columns(var)
restore


// *** // TABLE C17 // *** //
eststo clear
foreach var in remmedia school white gend {
qui eststo: reg `var' k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , robust
}
esttab , keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab remmedia school white gend if tempempr >= 21 & tempempr <= 22 & matched==1, s(mean) columns(var)

eststo clear
foreach var in hor age_dism cash ind_cons co {
qui eststo: reg `var' k kx x if tempempr >= 16 & tempempr <= 30 & matched==1 , robust
}
esttab , keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')
gstats tab hor age_dism cash ind_cons co if tempempr >= 21 & tempempr <= 22 & matched==1, s(mean) columns(var)


// *** // TABLE C18 // *** //
use "B2_mergeadmin.dta", clear

merge m:1 cpf using B5_racemode, keep(1 3) nogen keepus(racedismbmodemax)
replace race = racedismbmodemax if race != racedismbmodemax & racedismbmodemax !=.
drop racedismbmodemax

g large = pop > $pop

bys $varmatch: g co = _N
preserve

foreach csize in 1 3 5 7 10 15 20 25 { //   1 2 3 4 5 10 15 20 {

qui {
keep if co <= `csize'  

merge m:1 $varmatch using "B4_pnad_merge_collapse.dta", nogen keep(3) // race school



g emp_inf2 = emp_infor + emp_self
replace w = ln(w)

}

eststo clear
qui eststo e1: reg emp_form k kx x if tempempr >= 16 & tempempr <= 30 , vce(cluster id_survey)
qui eststo e2: reg emp_informal k kx x if tempempr >= 16 & tempempr <= 30 , vce(cluster id_survey)
qui eststo e3: reg emp_self k kx x if tempempr >= 16 & tempempr <= 30 , vce(cluster id_survey)
qui eststo e4: reg emp_inf2 k kx x if tempempr >= 16 & tempempr <= 30 , vce(cluster id_survey)

di "Admin mun-birthdate-gender-race level - matched sample - cluster size `csize' - $poplim"
esttab e1 e2 e3 e4, keep(k) cells(b(star fmt(3)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) title(`var')

su co
*/

eststo clear
restore, pres
}
restore, not




// *** // TABLE C1 // *** //
use A0_cash_base, clear

// Sample selection
keep if year >= 2011 & year <= 2014

keep if abs(x)<=0.4
keep if wkd >= 31  // Abono eligibility (1)
keep if year >= yf_min+5  // Abono eligibility (3)
drop if openjob==1

keep if tpv == 10
drop if natjuridica <= 2038
keep if hor >= 30
drop if ind == 1

*samples
g cash_sample = dt_dism >= mdy(6,1,year) & dt_dism <= mdy(7,15,year)
g uielig = tempempr>=6*30/7 & last_lay + dt_dism-mdy(1,1,year) > 488 

*vars
g white=race==2
g age_dism = (dt_dism-dtb)/365

foreach x in 3 6 36 { // everything monthly
*g haz`x' = und<=30/7 * `x' if und >= 30/7 * (`x'-1)
g sur`x' = und > 30/7 * (`x')
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

*stats
tabstat sur3 dur6 dur36 tempempr remmedia p_temp school white gend hor age_dism, c(s) stat(mean sd)
tabstat sur3 dur6 dur36 tempempr remmedia p_temp school white gend hor age_dism if cash_sample ==1, c(s) stat(mean sd) by(uielig)

ta cash_samp
ta uielig if cash_sample==1


// *** // TABLE C2 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* Balance of covariates
global outcome "tempempr remmedia p_tempempr school"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia

global outcome "white gend hor age_dism"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia


// *** // TABLE C3 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

*Prediction based on Covariates
g yob = year(dtb)
g age = dtb - dt_dism
replace school = school*10

global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"
foreach var in $outcome { // n_remmedia pre_remmedia 
cap drop `var'hat
qui areg `var' c.tempempr#c.tempempr i.year i.school i.race gend i.uf i.yob c.remmedr##c.remmedr##c.remmedr if abs(x)<=.1, absorb(sbc) //i.ind 
predict `var'hat, xb
}
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var'hat k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia


// *** // TABLE C4 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* first-stage
eststo clear
global outcome "cash"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} &year>=2012&year<=2014 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
qui eststo: reg `var' k kx x if abs(x)<= ${band} &year==2012 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
qui eststo: reg `var' k kx x if abs(x)<= ${band} &year==2013 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
qui eststo: reg `var' k kx x if abs(x)<= ${band} &year==2014 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 


// *** // TABLE C5 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* robust to specification
global outcome "cash sur3 sur36 dur6 dur36 n_jdur n_remdezr n_remmedr n_hor manager whitec migind totinc totmon totwage"

keep if abs(x)<=.2
foreach var in $outcome { // n_remmedia pre_remmedia 

eststo clear
global bandrob=0.01
qui eststo: reg `var' k if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.02
qui eststo: reg `var' k if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.03
qui eststo: reg `var' k if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.05
qui eststo: reg `var' k kx x if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.075
qui eststo: reg `var' k kx x if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.1
qui eststo: reg `var' k kx x if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.125
qui eststo: reg `var' k kx x if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.15
qui eststo: reg `var' k kx x kx2 x2 if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.175
qui eststo: reg `var' k kx x kx2 x2 if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)
global bandrob=0.2
qui eststo: reg `var' k kx x kx2 x2 if abs(x)<= ${bandrob} [w=(${bandrob}-abs(x))/${bandrob}], vce(cluster cpf)

esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)  noobs nonotes
*gstats tab `var' , s(mean) columns(var) // n_remmedia pre_remmedia 
}


// *** // TABLE C6 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

global outcome "cash sur3 sur36 dur6 dur36 n_jdur n_remdezr n_remmedr n_hor manager whitec migind totinc totmon totwage"

replace x = x + rnormal()/10^5 // correcting for little CCT bug so that bandwidth selection go through
foreach var in $outcome { // n_remmedia pre_remmedia 
qui replace `var' = -`var' // flip discontinuity sign

eststo clear

if "`var'"!= "cash" { // For 1 variable, CCT algorithm does go through with polynomial of order zero.
qui eststo: rdrobust `var' x , c(0.0000001) deriv(0) p(0) kernel(tri) vce(cluster cpf)
}
qui eststo: rdrobust `var' x , c(0.0000001) deriv(0) p(1) kernel(tri) vce(cluster cpf)
qui eststo: rdrobust `var' x , c(0.0000001) deriv(0) p(2) kernel(tri) vce(cluster cpf)

esttab , keep(RD_Estimate) b(a2) se(a2) star(* .1 ** .05 *** .01)  noobs nonotes
*gstats tab `var' , s(mean) columns(var) // n_remmedia pre_remmedia 
}


// *** // TABLE C7 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* comparison other studies
global outcome "jf20 jf52"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 



// *** // TABLE C8 // *** //
use C_cash_did, clear

global outcomes "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"
* main analysis - did 5 years requirement
eststo clear
foreach dep in $outcomes {
qui eststo: reg `dep' y5 i.year i.yf_min if elig==1 & x>=-.8, cl(cpf)
}
esttab , keep(y5) b(a2) se(a2) star(* .1 ** .05 *** .01) title(DiD - all t)  // nonotes
gstats tab $outcomes if elig==1 & x>=-.8, s(mean) columns(var) // n_remmedia pre_remmedia 


* placebo - shift eligibility req. from 5 to 3 years
g yplac = year>=yf_min+3
eststo clear
foreach dep in $outcomes {
qui eststo: reg `dep' yplac i.year i.yf_min if elig==1 & x>=-.8 & year>=yf_min+3-2 & year<=yf_min+3+1 , cl(cpf)
}
esttab , keep(yplac) b(a2) se(a2) star(* .1 ** .05 *** .01) title(DiD - placebo - within 4 years of lmt entry) // nonotes
gstats tab $outcomes if elig==1 & x>=-.8 & year>=yf_min+3-2 & year<=yf_min+3+1, s(mean) columns(var) // n_remmedia pre_remmedia 

* placebo - repeat main analysis on workers who are excluded because wage exceed 2 m.w. threshold
eststo clear
foreach dep in $outcomes {
qui eststo: reg `dep' y5 i.year i.yf_min if elig==0 & x>.0 & x<=.2, cl(cpf)
}
esttab , keep(y5) b(a2) se(a2) star(* .1 ** .05 *** .01) title(DiD - placebo - earning more than 2 m.w.)  // nonotes
gstats tab $outcomes if elig==0 & x>0 & x<=.2, s(mean) columns(var) // n_remmedia pre_remmedia 



// *** // TABLE C9 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

* UI Eligible vs Ineligible
cap drop ui1 ui2
g double tenmon = tempempr * (7/30) // tenure measured in months
g ui1 = (tenmon<6 | dt_dism-lastui< 470) // Non UI eligible
g ui2 = tenmon>=6 & tenmon<999999 & dt_dism-lastui> 488 // UI eligible

foreach gr in 1 2 {
eststo clear
global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage" // sur6
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} & ui`gr'==1 [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01) nonotes title(group `gr')
gstats tab $outcome if ui`gr'==1 & x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 
}

					
// *** // TABLE C10 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

keep if abs(x)<=.1
merge m:1 uf year gend using pnad200414_ufyeargender

replace school = school*10

*demeaning informality and unemployment rates
gegen i3 = mean(inform3)
gegen avunemp = mean(unemp)
replace inform3 = inform3 - i3
replace unemp = unemp - avunemp

global outcome "sur3 dur6 dur36 n_jdur n_remmedr totinc totmon totwage" //
global controls "i.school i.ind age_dism i.race gend i.hor c.remmedr##c.remmedr i.p_causadesli c.p_tempempr##c.p_tempempr" // i.qt_dism 

g wei = 1 - abs(x)/${band}

foreach var of global outcome {   
	
	eststo clear
	// Defin 3 - Consider self-employed as informal
	qui eststo: reg `var' k 1.k#c.inform3 kx x  inform3 c.x#c.inform3 1.k#c.x#c.inform3 if abs(x)<= ${band} [pw = wei], vce(cluster uf)
	
	qui eststo: reg `var' k 1.k#c.inform3 kx x  inform3 c.x#c.inform3 1.k#c.x#c.inform3 unemp 1.k#c.unemp c.x#c.unemp 1.k#c.x#c.unemp ///
	if abs(x)<= ${band} [pw = wei ], vce(cluster uf) // add unemp
	
	qui eststo: reghdfe `var' k 1.k#c.inform3 kx x  inform3 c.x#c.inform3 1.k#c.x#c.inform3 unemp 1.k#c.unemp c.x#c.unemp 1.k#c.x#c.unemp ${controls} ///
	if abs(x)<= ${band} [pw = wei ], vce(cluster uf) absorb(uf year) // add individual controls and fe
	
	esttab, keep(k 1.k#c.inform3) b(a2) se(a2) star(* .1 ** .05 *** .01) title() //inform3 1.k#c.unemp unemp

}



// *** // TABLE C11 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0


* only eligible workers
keep if x<0
drop k kx x

g t = dtb - mdy(7,1,year(dtb))
g k = t>=0
g kt = k * t


* 2011-14
global outcome "tempempr remmedia p_tempempr school white gend hor age_dism"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kt t if abs(t)<= ${bandrdbirth} , vce(cluster cpf) // [w=(${band}-abs(t))/${band}]
cap gegen av_`var' = mean(`var'), by(tbin)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if t > 0 & t <= ${bandrdbirth}, s(mean) columns(var) // n_remmedia pre_remmedia

* 2014
keep if year==2014
global outcome "tempempr remmedia p_tempempr school white gend hor age_dism"
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kt t if abs(t)<= ${bandrdbirth} , vce(cluster cpf) // [w=(${band}-abs(t))/${band}]
cap gegen av_`var' = mean(`var'), by(tbin)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if t > 0 & t <= ${bandrdbirth}, s(mean) columns(var) // n_remmedia pre_remmedia



// *** // TABLE C12 // *** //
use A_cash_othermonths, clear

keep if dt_dism<=mdy(5,31,year)

* measures censored at June 25th
g und_cens = min(und, (mdy(6,25,year)-dt_dism)/7)
foreach x in 1 2 3 4 5 { // everything monthly
g sur`x'_cens = und > (30/7*(`x'))
replace sur`x'_cens = 1 if dt_dism+und*7 >= mdy(6,25,year)
}

* Month of Dismissal grouped
cap drop gd
g gd =  1 if month(dt_dism)>=1 & month(dt_dism)<=1
replace gd = 2 if month(dt_dism)>=2 & month(dt_dism)<=3
replace gd = 3 if month(dt_dism)>=4 & month(dt_dism)<=5


* all workers
global outcome "und_cens sur1_cens sur2_cens sur3_cens sur4_cens sur5_cens" //
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01) nonotes title(Month of Dismissal `md')
gstats tab $outcome  if x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 

* groups by month of dismissal
forval gd = 1/3 {
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if abs(x)<= ${band} & gd==`gd' [w=(${band}-abs(x))/${band}], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01) nonotes title(Month of Dismissal group `gd')
gstats tab $outcome if gd==`gd' & x > 0 & x < 0.05, s(mean) columns(var) // n_remmedia pre_remmedia 
}


// *** // TABLE C13 // *** //

// *** // TABLE C13 - COLUMNS 1 2 // *** //
use A_cash, clear
* drop mass points at x=0
drop if x==0

g double tenmon = tempempr * (7/30) // tenure measured in months
keep if tenmon>=16 & tenmon<=30 // comparable tenure range to ui extension sample

g reg = int(uf/10)
tab reg, g(reg)

g mixed=race==8
g black=race==4

replace tempempr = tempempr*7/30
replace school = school/10
gstats tab remmedia tempempr age_dism gend school white black mixed reg1-reg5 serv sales constr manu , stats(mean sd)

// *** // TABLE C13 - COLUMNS 3 4 5 6 // *** //
use B_ui, clear

g reg = int(uf/10)
tab reg, g(reg)

g serv = ind==6
g sales = ind==5
g construc = ind==4
g manu = ind==3

g mixed=race==8
g black=race==4

replace school = school/10
gstats tab remmedia tempempr age_dism gend school white black mixed reg1-reg5 serv sales constr manu , stats(mean sd)
gstats tab remmedia tempempr age_dism gend school white black mixed reg1-reg5 serv sales constr manu [w=w_ebal], stats(mean sd)


// *** // TABLE C14 // *** //
use B_ui, clear

*Balance of Covariates
global outcome "remmedia school white gend"

eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} [w=w_ebal], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if tempempr>= 21 & tempempr<= 22 [w=w_ebal], s(mean) columns(var) // n_remmedia pre_remmedia 

global outcome "hor age_dism cash ind_cons"

eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} [w=w_ebal], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if tempempr>= 21 & tempempr<= 22 [w=w_ebal], s(mean) columns(var) // n_remmedia pre_remmedia 


// *** // TABLE C15 // *** //
use B_ui, clear

* Predicted Differences
global outcome "sur3 sur6 dur6 dur36 n_jdur n_remmedr totinc totmon totwage"
* time to find a new job - ebalance weights
foreach var in $outcome { // n_remmedia pre_remmedia 
cap drop `var'hat
qui reg `var' i.school i.ind i.qt_dism i.race gend i.hor i.year i.uf i.qt_rem i.mesdesli  i.abono
predict `var'hat, xb
}
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var'hat k kx x if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} [w=w_ebal], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if tempempr>= 21 & tempempr<= 22 [w=w_ebal], s(mean) columns(var) // n_remmedia pre_remmedia 



// *** // TABLE C16 // *** //
use B_ui, clear

* time to find a new job - probit weights
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} [w=weight], vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if tempempr>= 21 & tempempr<= 22 [w=weight], s(mean) columns(var) // n_remmedia pre_remmedia 

* time to find a new job - unweighted
eststo clear
foreach var in $outcome { // n_remmedia pre_remmedia 
qui eststo: reg `var' k kx x if tempempr >= 22 - ${bandui} & tempempr <= 24 + ${bandui} , vce(cluster cpf)
}
esttab , keep(k) b(a2) se(a2) star(* .1 ** .05 *** .01)   // nonotes
gstats tab $outcome if tempempr>= 21 & tempempr<= 22, s(mean) columns(var) // n_remmedia pre_remmedia 








cap log close